package com.luom.fsp.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class AESUtil {

    // AES 解密
    public static String decrypt(String encryptedData, String sessionKey, String iv) {
        try {
            // 如果加密数据使用了 URL-safe Base64 编码，将其转换为标准 Base64
            encryptedData = encryptedData.replace('_', '/').replace('-', '+');
            sessionKey = sessionKey.replace('_', '/').replace('-', '+');
            iv = iv.replace('_', '/').replace('-', '+');

            // Base64 解码
            byte[] encryptedDataBytes = Base64.getDecoder().decode(encryptedData);
            byte[] sessionKeyBytes = Base64.getDecoder().decode(sessionKey);
            byte[] ivBytes = Base64.getDecoder().decode(iv);

            // 创建 AES 解密器
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            SecretKeySpec key = new SecretKeySpec(sessionKeyBytes, "AES");
            cipher.init(Cipher.DECRYPT_MODE, key, new javax.crypto.spec.IvParameterSpec(ivBytes));

            // 解密数据
            byte[] decryptedData = cipher.doFinal(encryptedDataBytes);

            // 转换为字符串时，指定 UTF-8 编码，避免乱码
            return new String(decryptedData, StandardCharsets.UTF_8);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /*public static String decrypt(String encryptedData, String sessionKey) {
        try {
            // 使用Base64解码
            byte[] dataByte = Base64.getDecoder().decode(encryptedData);
            byte[] keyByte = Base64.getDecoder().decode(sessionKey);

            if (keyByte.length != 16) {
                throw new IllegalArgumentException("Invalid session key.");
            }

            SecretKeySpec keySpec = new SecretKeySpec(keyByte, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, keySpec);
            byte[] resultByte = cipher.doFinal(dataByte);

            return new String(resultByte, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }*/


//    public static String decrypt(String encryptedData, String sessionKey, String iv) {
//        try {
//            // URL Safe Base64 转换为标准 Base64
//            encryptedData = encryptedData.replace('-', '+').replace('_', '/');
//            iv = iv.replace('-', '+').replace('_', '/');
//
//            // Base64 解码
//            byte[] encryptedDataBytes = Base64.getDecoder().decode(encryptedData);
//            byte[] sessionKeyBytes = Base64.getDecoder().decode(sessionKey);
//            byte[] ivBytes = Base64.getDecoder().decode(iv);
//
//            // 创建 AES 解密器
//            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//            SecretKeySpec key = new SecretKeySpec(sessionKeyBytes, "AES");
//            if (ivBytes.length != 16) {
//                ivBytes = Arrays.copyOf(ivBytes, 16);  // 截取前16字节
//            }
//
//            cipher.init(Cipher.DECRYPT_MODE, key, new javax.crypto.spec.IvParameterSpec(ivBytes));
//
//            // 解密数据
//            byte[] decryptedData = cipher.doFinal(encryptedDataBytes);
//
//            // 转换为字符串
//            return new String(decryptedData, "UTF-8");
//        } catch (Exception e) {
//            e.printStackTrace();
//            return null;
//        }
//    }

}
